package com.example.huanhang.changzhouapp.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.base.BaseBackActivity;
import com.example.huanhang.changzhouapp.util.ThreadUtil;
import com.gyf.barlibrary.ImmersionBar;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePwdActivity extends BaseBackActivity {

    String regEx = "^[a-zA-Z]\\w{5,17}$";
    @BindView(R.id.update_pwd)
    AppCompatEditText updatePwd;
    @BindView(R.id.update_btn)
    AppCompatButton updateBtn;
    SharedPreferences sp;
    @Override
    public int getLayoutId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        setTitle("设置密码");
    }

    @Override
    public void initData() {
        sp = getSharedPreferences("login", MODE_PRIVATE);
    }
    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
    }
    @OnClick(R.id.update_btn)
    public void onViewClicked() {
        //密码
        String updateTrim = updatePwd.getText().toString().trim();
        String userName = sp.getString("userName", "");
        if (TextUtils.isEmpty(updateTrim)) {
            toast("密码不能为空，请重新输入");
        } else {
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(updateTrim);
            boolean rs = matcher.matches();
            if (rs) {
                updatePwd(userName, updateTrim);
            } else {
                toast("不符合密码规则，请重新输入");
            }
        }
    }

    private void updatePwd(String name, String pwd) {
        showWaitDialog("更新中...");
        ThreadUtil.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                String sql="UPDATE t_sys_person SET lccb_pwd='" + pwd + "' WHERE lccb_name='" + name + "'";
                int update = ThreadUtil.executeUpdate(sql);
                if (update == 1) {
                    Message message = updateHandler.obtainMessage(1);
                    updateHandler.sendMessage(message);
                } else {
                    Message message = updateHandler.obtainMessage(0);
                    updateHandler.sendMessage(message);
                }
            }
        });
    }

    private Handler updateHandler = new Handler(msg -> {
        int what = msg.what;
        switch (what) {
            case 1:
                toast("修改成功");
                dismissWaitDialog();
                activityFinish();
                break;
            case 0:
                toast("修改失败");
                dismissWaitDialog();
                break;
            default:
                break;
        }
        return false;
    });
}
